/**
 * Created by EEO on 2017/6/21.
 */
require(['init'], function () {
    var $bywechat = $('[name=bywechat]');
    var $bywechatTo = $('[name=bywechatTo]');
    var $byemail = $('[name=byemail]');
    var $byemailTo = $('[name=byemailTo]');
    var $bymessage = $('[name=bymessage]');
    var $bymessageTo = $('[name=bymessageTo]');
    var $ahead = $('[name=ahead]');
    var $aheadTo = $('[name=aheadTo]');
    var $perday = $('[name=perday]');
    var $perdayTo = $('[name=perdayTo]');
    var $rule = $('.rule');

    var goId;
    var doctorId;
    var teacheruid;
    var postData;
    var l = $('<span class="icon icon-size-16 icon-checkbox ahead" data-type="checkbox"><input type="checkbox" name="ahead"></span><span style="margin-left: 4px">' + i18next.t('notifyset.everybeforetheclass') + ' <input type="text" class="remind every">' + i18next.t('notifyset.minuteremindme') + ' </span>').appendTo($('.left'));
    var r = $('    <span class="icon icon-size-16 icon-checkbox perday" data-type="checkbox"> <input type="checkbox" name="perday"> </span> <span style="margin-left: 4px">' + i18next.t('notifyset.everyday') + '<input type="text" class="remind everyDay">' + i18next.t('notifyset.timeremindme') + '</span>').appendTo($('.right'));
    $.ajax({
        url: ' /api/user/current',
        success: function (data) {
            doctorId = data.data.userid;
            $.ajax({
                url: '/api/teacher/user/' + doctorId,
                success: function (data) {
                    teacheruid = data.data.teacheruid;
                    $.ajax({
                        url: '/api/notice/1/' + teacheruid,
                        success: function (data) {
                            goId = data.data[0].id;
                            //  获取数据  0 不选中   1选中
                            if (data.data[0].ahead != '0') {
                                $('.ahead').addClass('checked')
                            }
                            if (data.data[0].perday != '0') {
                                $('.perday').addClass('checked')
                            }
                            $('.every').val(data.data[0].ahead);
                            $('.everyDay').val(data.data[0].perday);
                            if (data.data[0].byemail == '1') {
                                $('.email').addClass('checked');
                                $byemail.val('1')
                            }
                            if (data.data[0].bywechat == '1') {
                                $('.wx').addClass('checked');
                                $bywechat.val('1')
                            }
                            var oInput = $('.every');
                            oInput.on('blur', function () {
                                var val = this.value;
                                var reg = /^[0-9]*$/;
                                if (reg.test(val)) {
                                    if (val > 750) {
                                        _Tips(i18next.t('notifyset.not12'))
                                        return
                                    }
                                } else {
                                    _Tips(i18next.t('notifyset.textnumber'))
                                    return
                                }

                            });
                            $('.remind_btn').on('click', function () {//点击保存 下面发请求
                                var val = $('.every').val();
                                var reg = /^[0-9]*$/;
                                if (reg.test(val)) {
                                    if (val > 750 && $('.ahead').hasClass('checked')) {
                                        _Tips(i18next.t('notifyset.not12'));
                                        return
                                    }
                                } else {
                                    if($('.ahead').hasClass('checked')){
                                        _Tips(i18next.t('notifyset.textnumber'));
                                        return
                                    }
                                }
                                var h = $('.everyDay').val();
                                var regNumber = /^([1-9]|1\d|2[0-4])$/;
                                if (!regNumber.test(h)&&$('.perday').hasClass('checked')) {
                                    _Tips(i18next.t('notifyset.remindtime'));
                                    return
                                }
                                if (!$('.perday').hasClass('checked')) {
                                    $('.everyDay').val('')
                                }
                                if (!$('.ahead').hasClass('checked')) {
                                    $('.every').val('')
                                }
                                if ($('.email').hasClass('checked')) {
                                    $byemail.val('1')
                                } else {
                                    $byemail.val('0')
                                }
                                if ($('.wx').hasClass('checked')) {
                                    $bywechat.val('1')
                                } else {
                                    $bywechat.val('0')
                                }
                                // 点击保存 返回给后台数据  0不选中 1选中
                                postData = {
                                    'configs[0][id]': goId,
                                    'configs[0][utype]': 1,
                                    'configs[0][uid]': teacheruid,
                                    'configs[0][ntype]': 1,
                                    'configs[0][byemail]': $byemail.val(),
                                    'configs[0][bymessage]': $bymessage.val(),
                                    'configs[0][bywechat]': $bywechat.val(),
                                    'configs[0][ahead]': $('.every').val(),
                                    'configs[0][perday]': $('.everyDay').val()
                                };
                                $.ajax({
                                    url: '/api/notice/1/' + teacheruid,
                                    type: 'post',
                                    data: $.extend({}, postData),
                                    success: function (data) {
                                        if(data.status.code==0){
                                            _Tips(i18next.t('table.success'))
                                            setTimeout(function(){
                                                window.location.reload();
                                            },500);
                                        }

                                    }
                                })
                            })

                        }
                    })
                }
            })
        }
    });
});